package com.dbpms.pj.sys.service.impl;

import com.dbpms.pj.common.annotation.RequiredLog;
import com.dbpms.pj.common.exception.ServiceException;
import com.dbpms.pj.common.pojo.PageObject;
import com.dbpms.pj.sys.dao.SysLogDao;
import com.dbpms.pj.sys.pojo.SysLog;
import com.dbpms.pj.sys.service.SysLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Song Yu
 * @Date: 2020/10/15 16:50
 */

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    SysLogDao sysLogDao;


    //点
    @RequiresPermissions("sys:user:update")
    @RequiredLog("日志查询")
    @Override
    public PageObject<SysLog> findPageObjects(String username,Integer pageCurrent){
        if(pageCurrent<=0||pageCurrent==null)
            throw new ServiceException("当前页码值不正确");
        int rowCount = sysLogDao.getRowCount(username);
        if(rowCount == 0)
            throw new ServiceException("没有找到对应记录");
        int pageSize = 5;
        int startIndex = (pageCurrent-1)*pageSize;
        List<SysLog> records = sysLogDao.findPageObjects(username,startIndex,pageSize);
        return new PageObject<>(pageCurrent,pageSize,rowCount,records);
    }

    @Override
    public int deleteObjects(Integer... ids) {
        if(ids==null||ids.length==0)
            throw new ServiceException("请先选择一条数据");
        int rows;
        try{
            rows=sysLogDao.deleteObjects(ids);
        }catch(Throwable e){
            e.printStackTrace();
            //发出报警信息(例如给运维人员发短信)
            throw new ServiceException("系统故障，正在恢复中...");
        }
        //4.对结果进行验证
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        //5.返回结果
        return rows;
    }

    @Override
    public void saveObject(SysLog entity) {
        sysLogDao.insertObject(entity);
    }
}
